package edu.wayne.cs.csc4110.hw6.framework.gui.lobby;

import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

import edu.wayne.cs.csc4110.hw6.framework.gui.inbox.InboxFrame;
import edu.wayne.cs.csc4110.hw6.framework.lobby.GameLobbyServer;
import edu.wayne.cs.csc4110.hw6.framework.plugin.GamePluginLoop;
import edu.wayne.cs.csc4110.hw6.framework.state.UserStateImpl;
import edu.wayne.cs.csc4110.hw6.interfaces.user.User;

public class MenuNavigation extends JPanel {

	private JButton inboxBtn;
	private JButton logoutBtn;
	private JButton gameToggleBtn;
	private User user;
	private JButton restartBtn;	
	private JFrame topFrame;

	public MenuNavigation(User user, final GamePluginLoop gamePluginLoop,
			final JFrame topFrame) {

		this.topFrame = topFrame;
		this.user = user;
		gameToggleBtn = new GameButton("Play!");
		inboxBtn = new GameButton("Inbox");
		logoutBtn = new GameButton("Logout");
		restartBtn = new GameButton("Restart");

		this.setLayout(new FlowLayout(FlowLayout.LEFT));
		gameToggleBtn.addActionListener(new GameLoopListener(gamePluginLoop));
		restartBtn.addActionListener(new GameLoopListener(gamePluginLoop));
		MenuListener menuListener = new MenuListener(gamePluginLoop);
		logoutBtn.addActionListener(menuListener);
		inboxBtn.addActionListener(menuListener);
		restartBtn.setVisible(false);
		add(gameToggleBtn);
		add(restartBtn);
		add(inboxBtn);
		add(logoutBtn);

	}

	private final class GameLoopListener implements ActionListener {
		private final GamePluginLoop gamePluginLoop;

		private GameLoopListener(GamePluginLoop gamePluginLoop) {
			this.gamePluginLoop = gamePluginLoop;
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(gameToggleBtn)) {
				if (gameToggleBtn.getText().equals("Play!")) {
					gamePluginLoop.start();
					gameToggleBtn.setText("Pause");
					restartBtn.setVisible(true);
				} else {
					if (gameToggleBtn.getText().equals("Pause")) {
						gamePluginLoop.pausePlugin();
						gameToggleBtn.setText("Resume");
					} else {
						gamePluginLoop.resumePlugin();
						gameToggleBtn.setText("Pause");
					}
				}
			} else if (e.getSource().equals(restartBtn)) {
				gamePluginLoop.restart();
				if (!gameToggleBtn.getText().equals("Play!"))
					gameToggleBtn.setText("Pause");
			}

		}
	}

	public final class MenuListener implements ActionListener {
		private GamePluginLoop gamePluginLoop;
		private User user;

		public MenuListener(GamePluginLoop gamePluginLoop) {
			this.gamePluginLoop = gamePluginLoop;
			this.user = gamePluginLoop.getPlugin().getUser();
		}

		@Override
		public void actionPerformed(ActionEvent e) {
			if (e.getSource().equals(logoutBtn)) {
				GameLobbyServer.getInstance().removeUser(user);
				topFrame.dispose();
				gamePluginLoop.stopPlugin();
				try {
					new UserStateImpl(user.getUsername(), gamePluginLoop.getPlugin().getUserExtension())
							.storeState(null, user);
				} catch (IOException e1) {
					JOptionPane.showMessageDialog(gameToggleBtn,
							"Could Not Save user State");
					e1.printStackTrace();
				}

			} else if (e.getSource().equals(inboxBtn)) {
				SwingUtilities.invokeLater(new Runnable() {
					@Override
					public void run() {
						new InboxFrame(user);
					}
				});
			}
		}
	}
}
